import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/util/toast_util.dart';
import 'package:liuxlcommon/view/lxl_widget.dart';
import 'package:liuxlcommon/view/my_style.dart';
import 'package:love_newf/app/com/view/my_app_bar.dart';

import '../../../../value/app_lstyle.dart' show rzBtnSty;
import '../../../com/base/L.dart';
import '../../../com/plugin/face_recognition_plugin.dart';
import '../../../com/util/permission_utils.dart';
import 'real_name_auth_logic.dart';

class RealNameAuthPage extends StatefulWidget {
  const RealNameAuthPage({Key? key}) : super(key: key);

  @override
  State<RealNameAuthPage> createState() => _RealNameAuthPageState();
}

class _RealNameAuthPageState extends State<RealNameAuthPage> {
  final RealNameAuthLogic logic = Get.put(RealNameAuthLogic());

  @override
  void initState() {
    super.initState();
    FaceRecognitionPlugin.init.then((v) {
      logic.faceSDKInitSuccess = v;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar("实名认证"),
      body: LColumn.eazy([
        LExpand(
          child: ListView(
            padding: EdgeInsets.all(20.w),
            children: [
              30.lsh,
              LInput(
                logic.name,
                lStyle: LStyle(
                  hintText: "请输入真实姓名！",
                  alignment: Alignment.center,
                ),
              ).css('''
          padding-left:12px;
          width: 317px;
          height: 52px;
          background: #F4F5F7;
          border-radius: 27px 27px 27px 27px;
      ''').onChange((text) => logic.name = text),
              20.lsh,
              LInput(
                logic.code,
                lStyle: LStyle(
                  hintText: "请输入身份证号码！",
                  alignment: Alignment.center,
                ),
              ).css('''
          padding-left:12px;
          width: 317px;
          height: 52px;
          background: #F4F5F7;
          border-radius: 27px 27px 27px 27px;
      ''').onChange((text) => logic.code = text),
              LText.easy(
                  "要求年满18-80岁，未满或超出将不能实名认证\n一个身份证信息只能实名一个账号\n证件信息绑定后不能修改，请填写本人实名信息，如非本人实名信息将无法体现(提交后请使用本人支付宝账号)")
            ],
          ),
        ),
        LText("提交").style(rzBtnSty).onTapL(() async {
          if (!await PermissionUtils.checkCameraPermission()) return;
          if (!await PermissionUtils.checkMicrophonePermission(
              message: "人脸认证，需要打开摄像头和麦克风权限")) {
            return;
          }
          if (!logic.faceSDKInitSuccess) {
            ToastUtils.toast("人脸初始化失败！");
            return;
          }
          var appconfig = await L.appConfig;
          var map = await FaceRecognitionPlugin.takeFace(
              appconfig.baiduAccessToken, logic.name, logic.code);
          if (map['data'] != null && map['data'].length > 0) {
            logic.submit(map['data'], map['xDeviceId'], map['sKey']);
          }
        })
      ]),
    );
  }

  @override
  void dispose() {
    Get.delete<RealNameAuthLogic>();
    FaceRecognitionPlugin.destroy;
    super.dispose();
  }
}
